<template>
  <div class="login">
    <el-form :model="myfrom" :rules="rules" class="myfrom" ref="form">
      <h3>iHRM 后台登录系统</h3>
      <el-form-item prop="mobile">
        <el-input
          v-model="myfrom.mobile"
          prefix-icon="el-icon-user-solid"
          placeholder="请输入手机号码"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="pwd">
        <el-input
          v-model="myfrom.password"
          prefix-icon="el-icon-lock"
          placeholder="请输入密码"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="loading" @click="loginClick"
          >登入</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { loginApi } from '@/api/user.js'
import { mapActions } from 'vuex'
import router from '@/router'
export default {
  data () {
    return {
      loading: false,
      myfrom: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              const bool = reg.test(value)
              bool ? callback() : callback(new Error('手机号不合法'))
            },
            trigger: 'blur'
          }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    ...mapActions({
      GET_TOKEN: 'user/GET_TOKEN'
    }),
    loginClick () {
      this.$refs.form.validate(async result => {
        if (result) {
          try {
            // this.loading = true
            await this.GET_TOKEN(this.myfrom)
            // this.$router.push('/')
            this.$message({
              message: '欢迎回家',
              type: 'success'
            })
            const _redirect = this.$route.query._redirect
            if (_redirect) {
              this.$router.push(_redirect)
            } else {
              this.$router.push('/')
            }
            this.loading = false
          } catch {
            console.log('验证失败llll')
          }
        } else {
          console.log('验证失败,hhh')
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login {
  background-image: url('./../../../src/assets/common/login.jpg');
  height: 100%;
  background-position: center;
  .myfrom {
    width: 400px;
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    button.el-button.el-button--primary {
      width: 400px;
      height: 50px;
      font-size: 24px;
      color: #fff;
    }
    h3 {
      font-size: 44px;
      font-weight: 200;
      letter-spacing: 1px; // 设置文本之间的间隔
      color: #fff;
      line-height: 1;
    }
    input.el-input__inner {
      height: 50px;
    }
  }
}
</style>
